package com.punch.system.controller;


import com.punch.common.dto.system.area.AreaDTO;
import com.punch.common.model.Result;
import com.punch.common.vo.system.area.AppAreaVO;
import com.punch.common.vo.system.area.TreeAreaVO;
import com.punch.system.service.NationAreaService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 全国地区表 前端控制器
 * </p>
 *
 * @author wjj
 * @since 2020-12-04
 */
@RestController
@RequestMapping("/sys")
public class NationAreaController {


    @Resource
    private NationAreaService nationAreaService;


    /**
     * 地区树形-pc端,地区管理
     *
     * @param parentId 地区父id,默认为0
     * @return List<TreeAreaVO>
     * @author wujj
     * @since 2020/6/4 19:16
     */
    @GetMapping(value = "/area/{parentId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "地区树形", notes = "地区树形", produces = "application/json")
    public Result<List<TreeAreaVO>> treeArea(@PathVariable Integer parentId) {
        return Result.succeed(nationAreaService.treeArea(parentId));
    }

    /**
     * 地区树形（根据状态查询）
     *
     * @param status   状态 0-停用 1-启用
     * @param parentId 父节点id
     * @return List<TreeAreaVO>
     * @author wujj
     * @since 2020/6/4 19:16
     */
    @GetMapping(value = "/area/{status}/{parentId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "地区树形", notes = "地区树形", produces = "application/json")
    public Result<List<TreeAreaVO>> treeAreaStatus(@PathVariable Integer status, @PathVariable Integer parentId, @RequestParam("type") Integer type) {
        return Result.succeed(nationAreaService.treeAreaStatus(status, parentId, type));
    }

    /**
     * 新增地区
     *
     * @param areaDTO 地区新增表单
     * @return areaId
     * @author wujj
     * @since 2020/6/4 19:33
     */
    @PostMapping(value = "/area", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增地区", notes = "新增地区", produces = "application/json; charset=utf-8")
    public Result<Integer> saveArea(@Valid @RequestBody AreaDTO areaDTO) {
        return Result.succeed(nationAreaService.saveArea(areaDTO));
    }


    /**
     * 修改地区
     *
     * @param areaDTO 地区更新表单
     * @return areaId
     * @author wujj
     * @since 2020/6/4 19:46
     */
    @PutMapping(value = "/area", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改地区", notes = "修改地区", produces = "application/json; charset=utf-8")
    public Result<Integer> updateArea(@Valid @RequestBody AreaDTO areaDTO) {
        return Result.succeed(nationAreaService.updateArea(areaDTO));
    }


    /**
     * 删除地区
     *
     * @param areaId 地区id
     * @return areaId
     * @author wujj
     * @since 2020/6/4 19:54
     */
    @DeleteMapping(value = "/area/{areaId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除地区", notes = "删除地区", produces = "application/json; charset=utf-8")
    public Result<Integer> deleteArea(@PathVariable Integer areaId) {
        return Result.succeed(nationAreaService.deleteArea(areaId));
    }

    /**
     * 地区信息
     *
     * @param areaId 地区id
     * @return TreeAreaVO
     * @author wujj
     * @since 2020/6/4 19:16
     */
    @GetMapping(value = "/area/feign/{areaId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "地区信息", notes = "地区信息", produces = "application/json")
    public TreeAreaVO getFeignArea(@PathVariable Integer areaId) {
        return nationAreaService.getFeignArea(areaId);
    }


    /**
     * 查找该地区id下的子id
     *
     * @param parentId 地区父id
     * @return List<Long>
     * @author wujj
     * @since 2020/6/4 19:16
     */
    @GetMapping(value = "/area/feign/child/{parentId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查找该地区id下的子id", notes = "查找该地区id下的子id", produces = "application/json")
    public List<Integer> listFeignChildId(@PathVariable Integer parentId) {
        return nationAreaService.listFeignChildId(parentId);
    }


    /**
     * 地区启用/停用
     */
    @PutMapping("/area/enable/{areaId}")
    @ApiOperation(value = "地区启用/停用")
    public Result enableArea(@PathVariable Integer areaId) {
        nationAreaService.enableArea(areaId);
        return Result.succeed("修改成功");
    }

    /**
     * app端地区
     *
     * @param parentId 地区父id,默认为0
     * @return List<TreeAreaVO>
     * @author wujj
     * @since 2020/6/4 19:16
     */
    @GetMapping(value = "/area/app/{parentId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "app端地区", notes = "app端地区", produces = "application/json")
    public Result<List<AppAreaVO>> appArea(@PathVariable Integer parentId) {
        return Result.succeed(nationAreaService.appArea(parentId));
    }

    /**
     * 获取父ids
     *
     * @param areaId 地区id
     * @return string
     */
    @GetMapping(value = "/area/feign/parent/{areaId}")
    public String getParentIds(@PathVariable("areaId") Integer areaId) {
        return nationAreaService.getParentIds(areaId);
    }
}
